package kr.hkit.powerjava.chapter14.work;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.BoxLayout;

public class ExPanel extends JPanel implements ActionListener{
	JLabel lbl;
	JLabel lbl2;
	JTextField inputFieldWon;
	JTextField inputFieldRate;
	JTextField resultField;
	JButton exBtn;
	JPanel jp1 = new JPanel();
	JPanel jp2 = new JPanel();
	
	public ExPanel() {
		init();
	}
	
	public void init(){
		lbl = new JLabel("원금을 입력하세요");
		inputFieldWon = new JTextField();
		inputFieldWon.setColumns(10);
		lbl2 = new JLabel("이율을 입력하세요");
		inputFieldRate = new JTextField();
		inputFieldRate.setColumns(10);
		
		exBtn = new JButton("변환");
		exBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
		exBtn.addActionListener(this);
		
		resultField = new JTextField();
		resultField.setColumns(15);
		
		jp1.setLayout(new BoxLayout(jp1,BoxLayout.Y_AXIS));
		
		jp1.add(lbl);
		jp1.add(inputFieldWon);
		jp1.add(lbl2);
		jp1.add(inputFieldRate);
		jp2.setLayout(new BoxLayout(jp2, BoxLayout.Y_AXIS));
		jp2.add(exBtn);
		jp2.add(resultField);
		
		add(jp1);
		add(jp2);
	}
	
	public String getInputFieldWon() {
		return inputFieldWon.getText();
	}
	public String getInputFieldRate() {
		return inputFieldRate.getText();
	}
	public void setResultField(String text) {
		resultField.setText(text);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("변환")){
			try{
			int won = Integer.parseInt(getInputFieldWon());
			double rate = Double.parseDouble(getInputFieldRate());
			setResultField("이자는 연"+ (int)(won*rate*0.01)+"만원입니다" );
			}catch(NumberFormatException err){
				JOptionPane.showMessageDialog(this, "숫자만 가능합니다");
			}
		}
	}
}
